---------------------------------------------------------------
-- @Description：         三号位逻辑
---------------------------------------------------------------
local skynet = require "skynet"
local log = require "sky_log"
local uu = require "sky_utils"
local gg = require "game_utils"

local M = {}

--有本回合花色
local function do_round_color(handcards, main_color, round_color, enemy_no_color_t,
        outcard_history, outcard_history_more, m_seatid)
    local card 

    local max_card, maxcard_seatid = gg.get_prev_max_outcard(outcard_history_more, main_color) 
    if not max_card then 
        log.error("[third] get_prev_max_outcard error!")
    end

    if uu.history_check_the_max_card(handcards, outcard_history, max_card) then
        card = uu.get_color_min_card(handcards, round_color)
        if card then
            uu.debug_logic(1)
            return card
        else
            log.error("[third] get_color_min_card_1 error!")
        end
    end

    if uu.check_is_friend(m_seatid, maxcard_seatid) then 
        if uu.history_check_the_max_card(handcards, outcard_history, max_card) then 
            card = uu.get_color_min_card(handcards, round_color)
            if card then
                uu.debug_logic(2)
                return card
            else
                log.error("[third] get_color_min_card_2 error!")
            end
        end
    end

    local get_t = gg.get_handcards_bigger_card_pure(handcards, max_card, round_color)
    if not get_t
    or not next(get_t) then
        card = uu.get_color_min_card(handcards, round_color)
        if card then
            uu.debug_logic(3)
            return card
        else
            log.error("[third] get_color_min_card_3 error!")
        end
    end

    if gg.check_is_lianxu(max_card, get_t) then
        card = uu.get_color_min_card(handcards, round_color)
        if card then
            uu.debug_logic(2)
            return card
        else
            log.error("[third] check_is_lianxu error!")
        end
    end

    if gg.check_enemy_has_no_color(enemy_no_color_t, round_color, m_seatid, outcard_history_more, true, handcards, outcard_history)==true then 
        card = uu.check_handcards_bigger_card_min(handcards, max_card)
        if card then
            uu.debug_logic(5)
            return card
        else
            card = uu.get_color_min_card(handcards, main_color)
            return card
        end
    else
        card = uu.check_handcards_bigger_card_max(handcards, max_card)
        if card then
            uu.debug_logic(19)
            return card
        else
            card = uu.get_color_min_card(handcards, main_color)
            log.error("[third] check_handcards_bigger_card_max error!")
        end
    end
end

--无本回合花色逻辑中可复用部分
local function no_round_color_common(handcards, main_color, enemy_no_color_t, outcard_history)
    local card

    local the_color = gg.check_enemy_has_no_color_s_and_main_card(handcards, {1,2,3,4}, enemy_no_color_t, main_color)
    if the_color then 
        local max_card = uu.history_check_max_card(handcards, the_color, outcard_history)
        local count = uu.get_color_cards_count(handcards, the_color)
        if max_card and count >= 2 then
            card = uu.get_color_min_card(handcards, the_color)
            if card then
                uu.debug_logic(6)
                return card
            else
                log.error("[third] get_color_min_card error!")
            end
        end
    end

    card = gg.history_check_more_color_or_min_card_expect_maincards(handcards, outcard_history, main_color)
    if not card then
        card = uu.get_color_min_card(handcards, main_color)
    end

    uu.debug_logic(7)
    return card
end

--无本回合花色
local function do_no_round_color(handcards, main_color, round_color, enemy_no_color_t, 
        outcard_history, outcard_history_more, m_seatid)

    local card

    local main_color_count = gg.get_main_card_count(handcards, main_color)
    if main_color_count==0 then 
        card = no_round_color_common(handcards, main_color, enemy_no_color_t, outcard_history)
       
        uu.debug_logic(8)
        return card
    end

    local max_card, maxcard_seatid = gg.get_prev_max_outcard(outcard_history_more, main_color) 
    if not max_card then 
        log.error("[third] get_prev_max_outcard error!")
    end

    --当前最大的牌是否是主牌
    local color = uu.calc_card_color(max_card)

    --不是主牌    
    if color~=main_color then
        if gg.check_enemy_has_no_color(enemy_no_color_t, main_color, m_seatid, outcard_history_more, true, handcards, outcard_history)==true then
        --if gg.check_enemy_has_no_color(enemy_no_color_t, main_color) then
            if uu.check_is_friend(m_seatid, maxcard_seatid) then 
                if uu.history_check_the_max_card(handcards, outcard_history, max_card) then 
                    card = no_round_color_common(handcards, main_color, enemy_no_color_t, outcard_history)
                    
                    uu.debug_logic(9)
                    return card
                end
            end
            card = uu.get_color_min_card(handcards, main_color)
            if card then
                uu.debug_logic(10)
                return card
            else
                log.error("[third] get_color_min_card error!")
            end
        else
            local t = uu.get_cards_except_t(handcards, {13, 14}, main_color)
            if next(t) then
                card = t[1]
                if card then
                    uu.debug_logic(11)
                    return card
                else
                    log.error("[third] get_cards_except_t error!")
                end
            else
                card = no_round_color_common(handcards, main_color, enemy_no_color_t, outcard_history)
                if card then
                    uu.debug_logic(12)
                    return card
                else
                    log.error("[third] no_round_color_common_1 error!")
                end
            end
        end
    end
        
    --是主牌
    local temp = gg.get_handcards_bigger_card_pure(handcards, max_card, round_color)

    --if uu.history_check_the_max_card(handcards, outcard_history, max_card) then 
    if (not temp) or (not next(temp)) then
        card = no_round_color_common(handcards, main_color, enemy_no_color_t, outcard_history)
        if card then
            uu.debug_logic(13)
            return card
        else
            log.error("[third] no_round_color_common_2 error!")
        end
    end

    if gg.check_enemy_has_no_color(enemy_no_color_t, round_color, m_seatid, outcard_history_more, true, handcards, outcard_history)==true then
        if gg.check_enemy_has_no_color(enemy_no_color_t, main_color, m_seatid, outcard_history_more, true, handcards, outcard_history)==true then
            card = uu.check_handcards_bigger_card_min(handcards, max_card, main_color)
            if card then
                local _color_ = uu.calc_card_color(card) 
                if _color_~=main_color then 
                    card = uu.get_color_min_card(handcards, main_color)
                end
                uu.debug_logic(14)
                return card
            else
                card = uu.get_color_min_card(handcards, main_color)
                --log.error("[third] no_round_color_common_3 error!")
            end
        end
    end

    --local temp = gg.get_handcards_bigger_card_pure(handcards, max_card, round_color)
    local _t = {}
    for _, v in pairs(temp or {}) do
        local value = uu.calc_card_value(v)
        if value~=13 and value~=14 then
            table.insert(_t, v)
        end
    end
    table.sort(_t, function (a, b)
        return a > b
    end)

    if _t and next(_t) then 
        if #_t==1 then
            card = _t[1]
        elseif #_t==2 then
            card = _t[1]
        elseif #_t==3 then
            card = _t[2]
        elseif #_t>=4 then
            card = _t[2]
        end
        if card then
            uu.debug_logic(15)
            return card
        else
            log.error("[third] get_cards_except_t_2 error!")
        end
    else
        local _value_ = uu.calc_card_value(max_card)
        if _value_ > 10 then 
            card = uu.check_handcards_bigger_card_max(handcards, max_card, main_color)
            if card then
                uu.debug_logic(16)
                return card
            else
                --log.error("[third] check_handcards_bigger_card_min error!")
                card = uu.get_color_min_card(handcards, round_color)
            end
        else
            card = no_round_color_common(handcards, main_color, enemy_no_color_t, outcard_history)
            if card then
                uu.debug_logic(17)
                return card
            else
                log.error("[third] no_round_color_common_4 error!")
            end
        end
    end
end

function M.do_card(handcards, main_color, round_color, enemy_no_color_t, outcard_history, outcard_history_more, m_seatid)
    if __LOG_LEVEL___==1 then
        local tmp = {}
        for _, v in ipairs(handcards) do
            table.insert(tmp, string.format("%04X", v))
        end
        uu.debug_logic(tmp)
    end
    
    local card

    if gg.check_has_round_color(handcards, round_color) then 
        card = do_round_color(handcards, main_color, round_color, enemy_no_color_t,
            outcard_history, outcard_history_more, m_seatid)
    else
        card = do_no_round_color(handcards, main_color, round_color, enemy_no_color_t, outcard_history,
            outcard_history_more, m_seatid)
    end
    
    --最后的容错
    local the_flag, the_card = gg.check_error_card(handcards, card, round_color, 3)
    if the_flag==true then
        uu.debug_logic(18)
        card = the_card
    end

    return card
end


return M